const cheerio = require('cheerio');
const request = require('request');
const fs = require('fs');
const rm = require('./lib/rm');
// const stream = require('stream');

let url = 'http://www.nmc.cn/publish/typhoon/message.html';
let ids = new Set();

const getDataIds = function (url) {
    return new Promise((resolve, reject) => {
        console.log(`对 ${url} 发起请求，获取报文ID列表。`)
        request(url, {method: 'GET'}, (error, response, body) => {
            console.log(`${url} 已响应...`);
            if (error) {
                reject(error);
            }
            let $ = cheerio.load(body.toString());
            let newIds = new Set();
            $('#mylistcarousel').find('> li > p.time').each((i, ele) => {
                let id = $(ele).data('id');
                if (!ids.has(id)) {
                    ids.add(id);
                    newIds.add(id);
                    console.log(id);
                }
            });
            if (newIds.size > 0) {
                console.log(`本次总计获取到 ${newIds.size} 条新报文。`)
            } else {
                console.log(`本次没有获取到新报文。`);
                reject('没有获取到新报文，本次运行结束！');
            }
            resolve(newIds);
        });
    });
};

getDataIds(url);